//
//  QuestionAnswerDetailController.m
//  HappyMother
//
//  Created by 易东 on 2017/10/26.
//  Copyright © 2017年 东哥. All rights reserved.
//

#import "QuestionAnswerDetailController.h"
#import "QuestionAnswerDetailHeadView.h"
#import "QuestionAnswerDetailCell.h"
#import "UITableView+SDAutoTableViewCellHeight.h"
#import "QuestionAnswerDetailModel.h"

@interface QuestionAnswerDetailController ()<UITableViewDelegate,UITableViewDataSource>

@property (strong,nonatomic) QuestionAnswerDetailHeadView *questionAnswerDetailHeadView;

@end

@implementation QuestionAnswerDetailController
{
    UITableView *showTableView;
    NSMutableArray *modelMarray;
}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self createNaviBarButton];
    self.title = @"问答详情";
    self.view.backgroundColor = [UIColor whiteColor];
    modelMarray = [NSMutableArray array];
    [self createUI];
    [self loadData];
}
#pragma mark -创建UI
-(void)createUI{
    
    //头部
    _questionAnswerDetailHeadView = [[[NSBundle mainBundle] loadNibNamed:@"QuestionAnswerDetailHeadView" owner:nil options:nil] firstObject];
    
    NSDictionary *dict = @{@"content":@"问题内容问题内容问题内容问题内容问题内容问题内容问题内容问题内容问题内容问题内容问题内容问题内容问题内容问题内容问题内容问题内容问题内容为 进口奶粉看风景呢啊的叫阿好的爸爸大家都把简单吧啊的那就看大家看大家淡定",@"image":@[@"pic1.jpg",@"pic4.jpg",@"pic1.jpg",@"pic4.jpg",@"pic4.jpg",@"pic2.jpg",@"pic3.jpg"]};
    _questionAnswerDetailHeadView.width = Screen_Width;
    _questionAnswerDetailHeadView.dataDict = dict;
    
    __weak typeof(self) weakself = self;
    
    _questionAnswerDetailHeadView.heightBlock = ^(CGFloat h) {
      
        NSLog(@"h--------%f",h);
        
        [weakself changeFrame:h];
        
    };

    //列表
    showTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,Screen_Width, Screen_Height-65)];
    
    showTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    showTableView.delegate = self;
    showTableView.dataSource = self;
    [self.view addSubview:showTableView];
    
    showTableView.tableHeaderView = _questionAnswerDetailHeadView;
    
    NSLog(@"初始--------%f",_questionAnswerDetailHeadView.height);
    
    [showTableView reloadData];
    
    

}
#pragma mark -改变frame
-(void)changeFrame:(CGFloat)h
{
    _questionAnswerDetailHeadView.height = h;
    [showTableView reloadData];
}
#pragma mark  -加载数据
-(void)loadData{
   
    QuestionAnswerDetailModel *model = [[QuestionAnswerDetailModel alloc] init];
    model.content = @"如果你无法简介的表达你的想法，";
    model.imagePathsArray = @[@"pic3.jpg",@"pic1.jpg",@"pic4.jpg",@"pic4.jpg",@"pic4.jpg"];
    
    QuestionAnswerDetailModel *model1 = [[QuestionAnswerDetailModel alloc] init];
    model1.content = @"如果你无法简介的表达你的想法，那就只能说明你还不够了解他啊 大溪地那就只能说明你还不够了解他啊 大溪地那就只能说明你还不够了解他啊 大溪地";
    model1.imagePathsArray = @[@"pic4.jpg"];
    

    QuestionAnswerDetailModel *model2 = [[QuestionAnswerDetailModel alloc] init];
    model2.content = @"如果你无法简介的表达你的想法那就只能说明你还不够了解他啊 大溪地，那就只能说明你还不够了解他啊 大溪地";
    model2.imagePathsArray = @[@"pic2.jpg",@"pic3.jpg",@"pic4.jpg",];
    
    
    QuestionAnswerDetailModel *model3 = [[QuestionAnswerDetailModel alloc] init];
    model3.content = @"那就只能说明你还不够了解他啊 大溪地那就只能说明你还不够了解他啊 大溪地";
    model3.imagePathsArray = @[@"pic1.jpg",@"pic4.jpg"];
    
    [modelMarray addObject:model];
    [modelMarray addObject:model1];
    [modelMarray addObject:model2];
    [modelMarray addObject:model3];
    
    [showTableView reloadData];
    
    
    
}
#pragma mark --- 代理
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return modelMarray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *_indetity = @"cellID";
    
    QuestionAnswerDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:_indetity];
    
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"QuestionAnswerDetailCell" owner:nil options:nil] firstObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    QuestionAnswerDetailModel *model = modelMarray[indexPath.row];
    cell.model = model;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    QuestionAnswerDetailModel *model = modelMarray[indexPath.row];

    return [showTableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[QuestionAnswerDetailCell class] contentViewWidth:[self cellContentViewWith]];
    
    /*
     *  第二种返回cell高度的方法，此方法不需要设置keyPath，适合一个cell与多个model的情况
     
     AutoCellHeightDataSettingBlock dataSetting = ^(UITableViewCell *cell) {
     DemoVC14Cell *myCell = (DemoVC14Cell *)cell;
     myCell.model = demoVC14Modelodel;
     };
     
     return [self.tableView cellHeightForIndexPath:indexPath
     cellClass:[DemoVC14Cell class]
     cellContentViewWidth:[UIScreen mainScreen].bounds.size.width cellDataSetting:dataSetting];
     */
}

- (CGFloat)cellContentViewWith
{
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    
    // 适配ios7横屏
    if ([UIApplication sharedApplication].statusBarOrientation != UIInterfaceOrientationPortrait && [[UIDevice currentDevice].systemVersion floatValue] < 8) {
        width = [UIScreen mainScreen].bounds.size.height;
    }
    return width;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
}
@end
